<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类列表</title>
    <link rel="stylesheet" href="/plugins/element-ui/index.css">
    <style>
        .tableBar {
            display: flex;
            justify-content: space-between;
        }
        .tableBox {
            margin-top: 20px;
            margin-bottom: 20px;
            border-bottom: solid 2px #f3f4f7;
            border-top: solid 2px #f3f4f7;
        }
        .pageList {
            display: flex;
            justify-content: center;
        }
        .non {
            color: red;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="member-app">
    <div class="container">
        <div class="tableBar">
            <div>
                <el-select v-model="level" placeholder="请选择分类级别">
                    <el-option label="全部分类" :value=null></el-option>
                    <el-option label="一级分类" value="0"></el-option>
                    <!--<el-option label="二级分类" value="-1"></el-option>-->
                    <el-option
                            v-for="item in topList"
                            :key="item.categoryId"
                            :label="item.categoryName"
                            :value="item.categoryId">
                    </el-option>
                </el-select>
            </div>
            <el-button
                    type="primary"
                    @click="addHandle('add')">
                + 添加分类
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                size="mini"
                class="tableBox">
            <el-table-column
                    prop="categoryImg"
                    label="分类图标"
                    align="center">
                <template slot-scope="scope">
                    <el-image style="width: auto; height: 50px; line-height: 50px; border: none; cursor: pointer;"
                              :src="getImage(scope.row.categoryImg)"
                              :preview-src-list="[ `/common/download?fileName=${scope.row.categoryImg}` ]" >
                        <div slot="error" class="image-slot">
                            <i class="el-icon-picture-outline"></i>
                        </div>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                    prop="categoryName"
                    label="分类名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="categoryTopName"
                    label="上级分类"
                    align="center">
            </el-table-column>

            <el-table-column
                    label="操作"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBut"
                            @click="addHandle(scope.row.categoryId)">
                        编辑
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="delBut non"
                            @click="removeHandle(scope.row.categoryId)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange">
        </el-pagination>
    </div>
</div>

<script src="/plugins/vue/vue.js"></script>
<script src="/plugins/element-ui/index.js"></script>
<script src="/plugins/axios/axios-0.18.0.js"></script>
<script src="/js/member.js"></script>
<script>
    new Vue({
        el: '#member-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 5,
                tableData : [],
                id: '',
                level: null,
                topList: []
            }
        },
        watch: {
            level(newVal) {
                this.page = 1
                this.init()
            },
        },
        methods: {
            init() {
                const params = {
                    pageNum: this.page,
                    pageSize: this.pageSize,
                    categoryTopId: this.level
                }
                axios.get('/category/query', {params}).then(res => {
                    let info = res.data
                    if (info.code === 1) {
                        this.tableData = info.data.records || []
                        this.counts = info.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            getTopList() {
                axios.get('/category/query?categoryTopId=0').then(res => {
                    let info = res.data
                    if (info.code === 1) {
                        this.topList = info.data.records || []
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            // 添加、修改
            addHandle (st) {
                if (st === 'add'){
                    window.location.href = '/page/category/add.html'
                } else {
                    window.location.href = '/page/category/add.html?id=' + st
                }
            },
            // 删除
            removeHandle(id) {
                this.$confirm("确认删除？", "提示", {type: "warning"}).then(() => {
                    axios.delete('/category/' + id).then(res => {
                        let info = res.data
                        if (info.code === 1) {
                            this.init()
                            this.$message.success('分类删除成功！')
                        } else {
                            this.$message.error('分类删除失败！')
                        }
                    })
                })
            },
            getImage (image) {
                return `/common/download?fileName=${image}`
            },
            handleSizeChange (val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange (val) {
                this.page = val
                this.init()
            }
        },
        created() {
            this.init()
            this.getTopList()
        },
    })
</script>
</body>
</html>
